package com.yuan.java.nettydemo;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import org.junit.Test;

/**
 * @author yuaneg 2020/1/10
 */
public class TimeServerClient {

    public void connect(int port, String host) {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //  拆包 和 粘包 的处理类
                            ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
                            //  响应数据转string 处理类
                            ch.pipeline().addLast(new StringDecoder());
                            ch.pipeline().addLast(new ChannelHandlerAdapter() {
                                /**
                                 * 发送数据
                                 */
                                @Override
                                public void channelActive(ChannelHandlerContext channelHandlerContext) {
                                    ByteBuf buffer;
                                    for (int i = 0; i < 100; ++i) {
                                        byte[] req = ("1" + System.getProperty("line.separator")).getBytes();
                                        buffer = Unpooled.buffer(req.length);
                                        buffer.writeBytes(req);
                                        channelHandlerContext.writeAndFlush(buffer);
                                    }
                                }

                                /**
                                 *  读取数据
                                 */
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                    System.out.println(msg);
                                }

                                @Override
                                public void exceptionCaught(ChannelHandlerContext ctx, Throwable ca) {
                                    ca.printStackTrace();
                                    ctx.close();
                                }
                            });
                        }
                    });
            ChannelFuture f = bootstrap.connect(host, port).sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }

    @Test
    public void main() {
        TimeServerClient timeServerClient = new TimeServerClient();
        timeServerClient.connect(3306, "127.0.0.1");
    }

}
